﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SafeLoad.NControl
{
  /// <summary>
  /// NProgress.xaml 的交互逻辑
  /// </summary>
  public partial class NProgress : UserControl
  {
    public NProgress()
    {
      InitializeComponent();
    }


    public static readonly DependencyProperty ValueProperty =
      DependencyProperty.Register("Value", typeof(int), typeof(NProgress),
        new PropertyMetadata(0, new PropertyChangedCallback(RenderUpdateByPropertyChanged)));
    public int Value
    {
      get { return (int)GetValue(ValueProperty); }
      set { SetValue(ValueProperty, value); }
    }

    /// <summary>
    /// 进度条颜色
    /// </summary>
    public static readonly DependencyProperty ProgressColorProperty =
      DependencyProperty.Register("ProgressColor", typeof(Color), typeof(NProgress),
        new PropertyMetadata(Colors.Tomato, new PropertyChangedCallback(RenderUpdateByPropertyChanged)));
    public Color ProgressColor
    {
      get { return (Color)GetValue(ProgressColorProperty); }
      set { SetValue(ProgressColorProperty, value); }
    }

    /// <summary>
    /// 进度条背景色
    /// </summary>
    public static readonly DependencyProperty BackColorProperty =
      DependencyProperty.Register("BackColor", typeof(Color), typeof(NProgress),
        new PropertyMetadata(Colors.Silver, new PropertyChangedCallback(RenderUpdateByPropertyChanged)));
    public Color BackColor
    {
      get { return (Color)GetValue(BackColorProperty); }
      set { SetValue(BackColorProperty, value); }
    }

    /// <summary>
    /// 进度条中间的颜色
    /// </summary>
    public static readonly DependencyProperty CenterColorProperty =
      DependencyProperty.Register("CenterColor", typeof(Color), typeof(NProgress),
        new PropertyMetadata(Colors.White, new PropertyChangedCallback(RenderUpdateByPropertyChanged)));
    public Color CenterColor
    {
      get { return (Color)GetValue(CenterColorProperty); }
      set { SetValue(CenterColorProperty, value); }
    }

    public static void RenderUpdateByPropertyChanged(DependencyObject dependency, DependencyPropertyChangedEventArgs e)
    {
      if (dependency is NProgress progress)
      {
        progress.Update();
      }
    }

    protected override void OnRender(DrawingContext drawingContext)
    {
      base.OnRender(drawingContext);
      Update();
    }

    public void Update()
    {
      canvas1.Children.Clear();
      int Width = (int)ActualWidth;
      int Height = (int)ActualHeight;
      int ProgressWidth = (Width * Value / 100);

      LinearGradientBrush linear = new LinearGradientBrush();
      linear.GradientStops = new GradientStopCollection(new List<GradientStop>() {
        new GradientStop(ProgressColor, 0.0f),
        new GradientStop(ProgressColor, 0.2f),
        new GradientStop(CenterColor, 0.6f),
        new GradientStop(ProgressColor, 1.0f)
      });
      linear.StartPoint = new Point(0, 0);
      linear.EndPoint = new Point(0, 1);

      WpfUtils.FillRectangle(canvas1, 0, 0, ProgressWidth, Height, linear);
    }

  }
}
